<template>
  <div>
      <el-form :inline="true" v-show="isSearchCollapse" class="query-form" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
            <!-- 搜索框-->
         <el-form-item prop="workOrderNumber">
                <el-input size="small" v-model="searchForm.workOrderNumber" placeholder="工单号" clearable></el-input>
         </el-form-item>
         <el-form-item prop="equipmentCode">
                <el-input size="small" v-model="searchForm.equipmentCode" placeholder="设备编码" clearable></el-input>
         </el-form-item>
         <el-form-item prop="factory">
                <el-input size="small" v-model="searchForm.factory" placeholder="厂" clearable></el-input>
         </el-form-item>
         <el-form-item prop="area">
                <el-input size="small" v-model="searchForm.area" placeholder="区域" clearable></el-input>
         </el-form-item>
         <el-form-item prop="mainEquipment">
                <el-input size="small" v-model="searchForm.mainEquipment" placeholder="主体设备" clearable></el-input>
         </el-form-item>
         <el-form-item prop="taskSource">
                <el-input size="small" v-model="searchForm.taskSource" placeholder="任务来源" clearable></el-input>
         </el-form-item>
         <el-form-item prop="roadworkContent">
                <el-input size="small" v-model="searchForm.roadworkContent" placeholder="施工内容" clearable></el-input>
         </el-form-item>
         <el-form-item prop="unsuccessfulPeople">
                <el-input size="small" v-model="searchForm.unsuccessfulPeople" placeholder="接办人" clearable></el-input>
         </el-form-item>
         <el-form-item prop="workNumber">
                <el-input size="small" v-model="searchForm.workNumber" placeholder="工号" clearable></el-input>
         </el-form-item>
         <el-form-item prop="completeTime">
                <el-input size="small" v-model="searchForm.completeTime" placeholder="完成时间" clearable></el-input>
         </el-form-item>
         <el-form-item prop="actualCompleteTime">
                <el-input size="small" v-model="searchForm.actualCompleteTime" placeholder="实际完成时间" clearable></el-input>
         </el-form-item>
         <el-form-item prop="completeSituation">
                <el-input size="small" v-model="searchForm.completeSituation" placeholder="完成情况" clearable></el-input>
         </el-form-item>
         <el-form-item prop="isAbnormal">
                <el-input size="small" v-model="searchForm.isAbnormal" placeholder="有无异常" clearable></el-input>
         </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="refreshList()" size="small">查询</el-button>
            <el-button @click="resetSearch()" size="small">重置</el-button>
          </el-form-item>
      </el-form>
        <!-- 导入导出-->
      <el-form :inline="true" v-show="isImportCollapse"  class="query-form" ref="importForm">
         <el-form-item>
          <el-button type="default" @click="downloadTpl()" size="small">下载模板</el-button>
         </el-form-item>
         <el-form-item prop="loginName">
            <el-upload
              class="upload-demo"
              :action="`${this.$http.BASE_URL}/flowform/form/projectPerform/import`"
              :on-success="uploadSuccess"
               :show-file-list="true">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只允许导入“xls”或“xlsx”格式文件！</div>
            </el-upload>
        </el-form-item>
      </el-form>
      <el-row>
       <!-- <el-button v-if="hasPermission('flowform:form:projectPerform:add')" type="primary" size="small" icon="el-icon-plus" @click="add()">新建</el-button>
        <el-button v-if="hasPermission('flowform:form:projectPerform:edit')" type="warning" size="small" icon="el-icon-edit-outline" @click="edit()"
         :disabled="dataListSelections.length != 1" plain>修改</el-button>
        <el-button v-if="hasPermission('flowform:form:projectPerform:del')" type="danger"   size="small" icon="el-icon-delete" @click="del()"
                  :disabled="dataListSelections.length <= 0" plain>删除
        </el-button>
        <el-button-group class="pull-right">
            <el-button
              type="default"
              size="small"
              icon="el-icon-search"
              @click="isSearchCollapse = !isSearchCollapse, isImportCollapse=false">
            </el-button>
            <el-button v-if="hasPermission('flowform:form:projectPerform:import')" type="default" size="small" icon="el-icon-upload2" title="导入" @click="isImportCollapse = !isImportCollapse, isSearchCollapse=false"></el-button>
            <el-button v-if="hasPermission('flowform:form:projectPerform:export')" type="default" size="small" icon="el-icon-download" title="导出" @click="exportExcel()"></el-button>
            <el-button
              type="default"
              size="small"
              icon="el-icon-refresh"
              @click="refreshList">
            </el-button>
        </el-button-group> -->
      </el-row>
    <el-table
      :data="dataList"
      border
      size="medium"
      @selection-change="selectionChangeHandle"
      @sort-change="sortChangeHandle"
      v-loading="loading"
      class="table">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
    <el-table-column
        prop="workOrderNumber"
        show-overflow-tooltip
        sortable="custom"
        label="工单号">
            <template slot-scope="scope">
              <el-link  type="primary" :underline="false" v-if="hasPermission('flowform:form:projectPerform:edit')" @click="edit(scope.row.id)">{{scope.row.workOrderNumber}}</el-link>
              <el-link  type="primary" :underline="false" v-else-if="hasPermission('flowform:form:projectPerform:view')"  @click="view(scope.row.id)">{{scope.row.workOrderNumber}}</el-link>
              <span v-else>{{scope.row.workOrderNumber}}</span>
            </template>
      </el-table-column>
    <el-table-column
        prop="equipmentCode"
        show-overflow-tooltip
        sortable="custom"
        label="设备编码">
      </el-table-column>
    <el-table-column
        prop="factory"
        show-overflow-tooltip
        sortable="custom"
        label="厂">
      </el-table-column>
    <el-table-column
        prop="area"
        show-overflow-tooltip
        sortable="custom"
        label="区域">
      </el-table-column>
    <el-table-column
        prop="mainEquipment"
        show-overflow-tooltip
        sortable="custom"
        label="主体设备">
      </el-table-column>
    <el-table-column
        prop="taskSource"
        show-overflow-tooltip
        sortable="custom"
        label="任务来源">
        <template slot-scope="scope">
         {{ $dictUtils.getDictLabel("repair_source", scope.row.taskSource) }}
        </template>
      </el-table-column>
    <el-table-column
        prop="roadworkContent"
        show-overflow-tooltip
        sortable="custom"
        label="施工内容">
      </el-table-column>
      <el-table-column
          prop="workTime"
          show-overflow-tooltip
          sortable="custom"
          label="工时">
        </el-table-column>
        <el-table-column
            prop="state"
            show-overflow-tooltip
            sortable="custom"
            label="状态">
            <template slot-scope="id">
              {{ $dictUtils.getDictLabel("repair_statement", id.row.state)}}
            </template>
          </el-table-column>
          <el-table-column
              prop="man"
              show-overflow-tooltip
              sortable="custom"
              label="项目协办人">
            </el-table-column>
         <el-table-column
              prop="overhaulPeople"
              show-overflow-tooltip
              sortable="custom"
              label="实际工时">
            </el-table-column>
         <el-table-column
             prop="repirpeople"
             show-overflow-tooltip
             sortable="custom"
             label="实际人数">
           </el-table-column>
    <el-table-column
        prop="people"
        show-overflow-tooltip
        sortable="custom"
        label="接办人">
      </el-table-column>
    <el-table-column
        prop="workNumber"
        show-overflow-tooltip
        sortable="custom"
        label="工号">
      </el-table-column>
    <el-table-column
        prop="completeTime"
        show-overflow-tooltip
        sortable="custom"
        label="完成时间">
      </el-table-column>
      <el-table-column
          prop="startTime"
          show-overflow-tooltip
          sortable="custom"
          label="检修项目开始时间">
        </el-table-column>
    <el-table-column
        prop="actualCompleteTime"
        show-overflow-tooltip
        sortable="custom"
        label="实际完成时间">
      </el-table-column>
    <el-table-column
        prop="completeSituation"
        show-overflow-tooltip
        sortable="custom"
        label="完成情况">
      </el-table-column>
    <el-table-column
        prop="isAbnormal"
        show-overflow-tooltip
        sortable="custom"
        label="有无异常">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        fixed="right"
        width="200"
        label="操作">
        <template  slot-scope="scope">
          <el-button v-if="hasPermission('flowform:form:projectPerform:view')" type="text" icon="el-icon-view" size="small" @click="view(scope.row.id)">查看</el-button>
          <el-button type="text" icon="el-icon-help" size="small" @click="changestate('changestate', scope.row.id)">确认</el-button>
          <el-button v-if="hasPermission('flowform:form:projectPerform:edit')" type="text" icon="el-icon-edit" size="small" @click="edit(scope.row.id)">填报</el-button>
          <!-- <el-button v-if="hasPermission('flowform:form:projectPerform:del')" type="text"  icon="el-icon-delete" size="small" @click="del(scope.row.id)">删除</el-button> -->
          <el-button type="text"  icon="el-icon-edit" size="small" @click="editTimes(scope.row.id, 'editTimes')">开始</el-button>
          <el-button type="text"  icon="el-icon-edit" size="small" @click="editTime(scope.row.id, 'editTime')">完成</el-button>
          <el-button v-if='isSubmit' type="text" icon="el-icon-edit" size="small" @click="goto(scope.row.processId, '提交')">提交</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageNo"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="total"
      background
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
        <!-- 弹窗, 新增 / 修改 -->
    <ProjectPerformForm  ref="projectPerformForm"  @refreshDataList="refreshList" ></ProjectPerformForm>
  </div>
</template>

<script>
  import ProjectPerformForm from './ProjectPerformForm'
  import pick from 'lodash.pick'
  export default {
    data () {
      return {
        searchForm: {
          workOrderNumber: '',
          equipmentCode: '',
          factory: '',
          area: '',
          mainEquipment: '',
          taskSource: '',
          roadworkContent: '',
          unsuccessfulPeople: '',
          workNumber: '',
          completeTime: '',
          actualCompleteTime: '',
          completeSituation: '',
          startTime: '',
          workTime: '',
          state: '',
          projectMan: '',
          isAbnormal: '',
          testId: '',
          overhaulPeople: '',
          man: '',
          repirpeople: '',
          processId: ''
        },
        dataList: [],
        pageNo: 1,
        pageSize: 10,
        total: 0,
        orderBy: '',
        dataListSelections: [],
        isSearchCollapse: false,
        isImportCollapse: false,
        loading: false,
        isFlowableName: '', // 对应流程名
        isTitle: '', // 对应流程标题
        isSubmit: false
      }
    },
    components: {
      ProjectPerformForm
    },
    activated () {
      this.refreshList()
      this.$http({
         url: `/sys/log/logId`,
         method: 'get'
       }).then(({data}) => {
        if (data.logId.roleNames === '部门管理员' || data.logId.roleNames === '项目负责人') {
          this.isSubmit = true
        }
        })
    },

    methods: {
      goto (processId, name) {
        if (name === '提交') {
          this.isFlowableName = '项目负责人流程'
          this.isTitle = '【项目负责人流程】'
        }
        this.loading = true
        this.$http({
          url: '/flowable/task/todoflowableRepair',
          method: 'get',
          params: {
            flowableName: this.isFlowableName,
            tableName: 'repair_task_distribution_process',
            tableId: processId
          }
        }).then(({data}) => {
          if (data.page.list.length === 0) {
            this.loading = false
            this.$message(`当前没有【${this.isFlowableName}】这条流程`, '提示', {
              type: 'warning'
            })
          } else {
          this.$http.get('/flowable/task/getTaskDef', {params: {
            taskId: data.page.list[0].task.id,
            taskName: data.page.list[0].task.name,
            taskDefKey: data.page.list[0].task.taskDefinitionKey,
            procInsId: data.page.list[0].task.processInstanceId,
            procDefId: data.page.list[0].task.processDefinitionId,
            status: data.page.list[0].status
          }}).then(({data}) => {
            if (data.success) {
              this.$router.push({
                path: '/flowable/task/TaskForm',
                query: {formTitle: '检修流程', outPutForm: '', title: `审批${this.isTitle}`, ...pick(data.flow, 'formType', 'formReadOnly', 'formUrl', 'procDefKey', 'taskDefKey', 'procInsId', 'procDefId', 'taskId', 'status', 'title', 'businessId', 'taskName')}
              })
              this.loading = false
            }
          })
          }
        })
      },
       // 修改状态
      changestate (name, changestate) {
        changestate = changestate || this.dataListSelections.map(item => {
          return item.changestate
        })[0]
        this.$refs.projectPerformForm.init(name, changestate)
      },
      // 获取数据列表
      refreshList () {
        this.loading = true
        this.$http({
          url: '/flowform/form/projectPerform/list',
          method: 'get',
          params: {
            'pageNo': this.pageNo,
            'pageSize': this.pageSize,
            'orderBy': this.orderBy,
            ...this.searchForm
          }
        }).then(({data}) => {
          if (data && data.success) {
            console.log(data)
            this.dataList = data.page.list
            // var a = 1
            // for (let var1 in this.dataList) {
            //                 if (this.dataList[var1].projectMan.indexOf(',') !== -1) {
            //                   this.dataList[var1].projectMan.split(',').forEach((id) => {
            //                     this.$http.get(`/sys/user/queryById?id=${id}`).then(({data}) => {
            //                       if (data.user && data.user.id !== '') {
            //                         if (this.dataList[var1].man === undefined) {
            //                            this.dataList[var1].man = ''
            //                         }
            //                         if (a === this.dataList.length) {
            //                          this.dataList[var1].man += data.user.name
            //                         } else {
            //                           this.dataList[var1].man += data.user.name + ','
            //                             a = a + 1
            //                         }
            //                       }
            //                     })
            //                   })
            //                 }
            //               }
            // this.total = data.page.count
            this.loading = false
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageNo = 1
        this.refreshList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageNo = val
        this.refreshList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },

    // 排序
      sortChangeHandle (obj) {
        if (obj.order === 'ascending') {
          this.orderBy = obj.prop + ' asc'
        } else if (obj.order === 'descending') {
          this.orderBy = obj.prop + ' desc'
        } else {
          this.orderBy = ''
        }
        this.refreshList()
      },
      // 新增
      add () {
        this.$refs.projectPerformForm.init('add', '', '')
      },
      // 修改时间
      editTime (id, name) {
          var date = new Date()
          var year = date.getFullYear() + ''
          var month = date.getMonth() + 1 + ''
          var strDate = date.getDate() + ''
          var times = year + '/' + month + '/' + strDate + ' ' + (date + '').split(' ')[4]
          id = id || this.dataListSelections.map(item => {
            return item.id
          })[0]
          this.$refs.projectPerformForm.initDosubmit(name, id, times)
      },
      // 开始时间
      editTimes (id, name) {
          var date = new Date()
          var year = date.getFullYear() + ''
          var month = date.getMonth() + 1 + ''
          var strDate = date.getDate() + ''
          var times = year + '/' + month + '/' + strDate + ' ' + (date + '').split(' ')[4]
          id = id || this.dataListSelections.map(item => {
            return item.id
          })[0]
          this.$refs.projectPerformForm.initDosubmit(name, id, times)
      },
      // 修改
      edit (id) {
        id = id || this.dataListSelections.map(item => {
          return item.id
        })[0]
        this.$refs.projectPerformForm.init('edit', id, '')
      },
      // 查看
      view (id) {
        this.$refs.projectPerformForm.init('view', id, '')
      },
      // 删除
      del (id) {
        let ids = id || this.dataListSelections.map(item => {
          return item.id
        }).join(',')
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/flowform/form/projectPerform/delete',
            method: 'delete',
            params: {'ids': ids}
          }).then(({data}) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.refreshList()
            }
            this.loading = false
          })
        })
      },
      // 导入成功
      uploadSuccess (res, file) {
        if (res.success) {
          this.$message.success({dangerouslyUseHTMLString: true,
            message: res.msg})
        } else {
          this.$message.error(res.msg)
        }
      },
      // 下载模板
      downloadTpl () {
        this.$utils.download('/flowform/form/projectPerform/import/template')
      },
      exportExcel () {
        this.$utils.download('/flowform/form/projectPerform/export')
      },
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.refreshList()
      }
    }
  }
</script>
